<?php


class ProductsAction extends Action{
	
	//客户喜爱
	function HotSellProject(){
		 
		$sql = " select b.projectid as ID, c.ProjectName as ProjectName, b.StarNumber star,
   b.Remarks as Remarks, b.Price as Price  from SS_HotSellProjectMain a
 left join SS_HotSellProjectDetail b on a.mdid = b.mdid
 left join bd_ProjectSet c on c.ID = b.projectid
 where TableType = '1' ";
		 
		$ob = M();
		$list = $ob->query($sql);
		echo Encode($list);
		 
	}
	
	//店长推荐
	function HotSellProjectByMaster(){
	
		$sql = " select b.projectid as ID, c.ProjectName as ProjectName, b.StarNumber star,
   b.Remarks as Remarks, b.Price as Price  from SS_HotSellProjectMain a
 left join SS_HotSellProjectDetail b on a.mdid = b.mdid
 left join bd_ProjectSet c on c.ID = b.projectid
 where TableType = '0' ";
	
		$ob = M();
		$list = $ob->query($sql);
		echo Encode($list);
	
	}
	
	function HealthClass(){
// 		select id, kindNo, pid, kindName from bd_CusHealthTraceClass order by kindNo
// 		select id, kindId, name, ContentType from bd_CusHealthTracestatus order by kindId

		$blist = S('Health');
		if(!empty($blist)){
			echo Encode($blist);
			return ;
		}
		
		//大类
		$sql1 = "select id, kindNo, pid, kindName from bd_CusHealthTraceClass 
				where len(kindNo) = 3 order by kindNo";
		
		//中类
		$sql2 = "select * from bd_CusHealthTraceClass 
				where len(kindNo) <> 3 order by kindNo";
		
		//小类
		$sql3 = "select b.* from bd_CusHealthTraceClass a left join bd_CusHealthTracestatus b
			on a.id = b.kindId where len(a.kindNo) <> 3 order by b.kindId,b.id";
		
		$ob = M();
		$blist = $ob->query($sql1);
		$mlist = $ob->query($sql2);
		$slist = $ob->query($sql3);
		

		foreach($mlist as $key=>$value){
			
			foreach($slist as $key1=>$value1){
				if($value1['kindId']==$value['id']){
					$mlist[$key]['detail'][] = $value1;
					unset($slist[$key1]);
				}
			}
			
		}
		
		foreach($blist as $key=>$value){
			foreach($mlist as $key1=>$value1){
				if($value1['pid']==$value['id']){
					$blist[$key]['detail'][] = $value1;
					unset($mlist[$key1]);
				}
			}
		}
		
		S('Health',$blist,3600);
		echo Encode($blist);
	}
	
	function HealthSave(){
		
		$return = array();
		
		$cid = $_GET['cid'];
		$name = $_GET['name'];
		$ids = explode(',', $_GET['ids']);
		
		if(empty($ids)){
			return ;
		}
		
		$sqlc = "select ID from SS_CustomerStatus order by ID desc ";
		$ob = M();
		$lastrow = $ob->query($sqlc);
		
		$ID = $lastrow[0]['ID'];
		$tmp = substr($ID,1,7);
		$ID = '1'.str_pad(($tmp+1),7,'0',STR_PAD_LEFT).rand(10,99);
		$TableNo = 'WX'.substr($ID, 2);
		$SubID = substr($ID,0,8).'00';
		
		$sql = "Insert into SS_CustomerStatus(ID, TableNo, CustomerId, 
			CustomerName, IssueUser, IssueDate, fstate)
         Values('{$ID}', '{$TableNo}', '{$cid}', 
			'{$name}', '{$name}', getdate(), 0)";
		
// 		$return[] = $sql;
		$ob->execute($sql);
		
		$sql1 = "select c.name as cname, c.id as cid,a.kindNo as mid,a.kindname as mname,a.pid
				 from bd_CusHealthTracestatus c 
					left join bd_CusHealthTraceClass a 
						on c.kindId = a.id where  c.id in ({$_GET['ids']})";
		
		$sql2 = "select id, kindNo, pid, kindName from bd_CusHealthTraceClass 
				where len(kindNo) = 3 order by kindNo"; 
		
		$istList = M()->query($sql1);
		$bClassList = M()->query($sql2);
		
		foreach($istList as $key=>$value){
			foreach($bClassList as $key1=>$value1){
				if($value['pid']==$value1['id']){
					$istList[$key]['LargeKindID'] = $value1['kindNo'];
					$istList[$key]['LargeKindName'] = $value1['kindName'];
				}
			}
			
		}
		
		foreach($istList as $key=>$value){
			$sqltmp = "insert into SS_CustomerStatusDetail
					(SubID, MDID, LargeKindID, LargeKindName,
         	SubKindID, SubKindName, StatusID, StatusName, 
         	IssueUser, IssueDate, StatusContent)
  Values('".(++$SubID)."', '".($ID)."', '".($value['LargeKindID'])."', '".($value['LargeKindName'])."', 
         		 '".($value['mid'])."', '".($value['mname'])."', '".($value['cid'])."','".($value['cname'])."',  
         		'".$name."', getdate(), 'Check')";

			$ob->execute($sqltmp);
// 			$return[] = $sqltmp;
		}
		
		echo Encode(true);
		
	}
	
	function HealthReport(){
		
		$sql = "    Select distinct b.CustomerId as 会员ID,a.LargeKindID as 一级ID, a.LargeKindName as LargeKindName, a.SubKindID as SubKindID, a.SubKindName as SubKindName, 
    a.StatusID, a.StatusName as StatusName, b.issuedate, a.StatusContent, c.ContentType, d.ProjectID as ProjectID, e.ProjectName as ProjectName, 
    e.Brand, f.ProjectKindName as ProjectKindName
    from (Select top 1 * from SS_CustomerStatus Where 1=1 and  CustomerId = '0000113158'  order by issuedate desc ) b 
    left join SS_CustomerStatusDetail a on a.MDID = b.ID 
    left join bd_CusHealthTracestatus c on c.id = a.StatusID 
    left join bd_CusHealthTracestatusProject d on c.id = d.Mdid 
    inner join bd_Projectset e on d.projectid = e.id 
    left join bd_Projectkinds f on e.ProjectKindNo = f.ProjectKindNo 
    where 1=1 
    and not exists(select * from bd_projectandtimes aa 
               left join bd_carddetail bb on aa.carddetailid = bb.id 
               where bb.customerid = b.customerid and d.projectid = aa.projectid)
    order by b.CustomerId,a.LargeKindID, a.SubKindID, a.StatusID, d.Projectid    ";
		
		$id = $_GET['id'];
		$ob = M();
		$info = $ob->query(str_replace(
				array('0000113158'),
				array($id),
				$sql));
		 
		echo Encode($info);
		
	}
	
	
	
	
	
	
	
	
	
	
	
}



